import os
import pickle

class DragonManager(object):
    triggers = ["!newdragon", "!slayers", "!listdragons", "!dragon"]
    channels = {}
    name = "dragon"
    help = ["Province listing commands:",
            "%c6!dragon%c"%(03,03),
            "%c6!newdragon%c <life-points>"%(03,03),
            "%c6!slayers%c <dragon-number>"%(03,03),
            "%c6!listdragons%c"%(03,03)]
    
    def command(self, firstword, user, channel, msg):
        if not self.channels.has_key(channel):
            self.channels[channel] = DragonList(channel)        
        dragonlist = self.channels[channel]
        
        if msg.lower() == "!listdragons":
            return {'chanmsg': dragonlist.list()}
        if msg.lower() == "!dragon":
            return {'chanmsg': [dragonlist.current()]}
        if firstword == "!newdragon":
            words = msg.split()
            if len(words) != 2: return {'chanmsg':["Usage: !newdragon <life-points>"]}
            points = words[1].replace(',','')
            try: 
                points = int(points)
            except: 
                return {'chanmsg':["Usage: !newdragon <life-points>"]}
            return {'chanmsg': [dragonlist.new(points)]}
        return {'usermsg': ["Command not yet handled"]}
    

class DragonList(object):
    def __init__(self, channel):
        self.dragons = []
        self.filename = ".\\" + channel + "_dragons.txt"
        if os.path.exists(self.filename):
            f = open(self.filename, 'r')
            self.dragons = pickle.load(f)
            f.close()    
    
    def save(self):
        f = open(self.filename, 'w')
        pickle.dump(self.dragons, f)
        f.close()

    def current(self):
        if len(self.dragons) == 0: return "No dragon listed."
        return self.dragons[-1].info()
    
    def list(self):
        if len(self.dragons) == 0: return ["No dragons listed."]
        return map(lambda d: d.info(), self.dragons)

    def new(self, pointsleft):
        if len(self.dragons) >= 10: return "Maximum of 10 dragons already reached."
        self.dragons.append(Dragon(pointsleft))
        self.save()
        return "Added new dragon with %d strength points."%(pointsleft)

    def remove(self, number):
        if len(self.dragons) < number: return "No dragon number %d."%(number)
        del self.dragons[number-1]
        self.save()
        return "Removed dragon listing for dragon #%d."%(number)


class Dragon(object):    
    def __init__(self, life):
        self.hits = []
        self.life = life
        self.sender = "unknown"
        self.type = "unknown"
        
    def claim(self, player, points, pointsleft):
        self.hits.append(Hit(player, points, pointsleft))
    
    def info(self):
        return "%s dragon from %s"%(self.type, self.sender)
    
    
class Hit(object):
    def __init__(self, player, points, pointsleft):
        self.player = player
        self.points = points
        self.poinstleft = pointsleft
        self.pointsbefore = pointsleft + points